<template>
  <div>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="提交反馈" name="submit" v-if="!hasSubmitted">
        <el-form :model="form" ref="form" :rules="rules" label-width="80px" class="feedback-form">
          <el-form-item label="评分" prop="rating">
            <el-rate
              v-model="form.rating"
              :colors="['#99A9BF', '#F7BA2A', '#FF9900']"
              :texts="['很差', '较差', '一般', '较好', '很好']"
              show-text>
            </el-rate>
          </el-form-item>
          <el-form-item label="评价" prop="comment">
            <el-input
              type="textarea"
              v-model="form.comment"
              :rows="4"
              placeholder="请输入您对本次会议的评价和建议">
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm">提交反馈</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      
      <el-tab-pane label="反馈统计" name="statistics">
        <div v-loading="statisticsLoading">
          <div v-if="statistics.totalFeedbacks > 0" class="statistics-container">
            <div class="statistics-overview">
              <div class="statistics-item">
                <div class="statistics-label">平均评分</div>
                <div class="statistics-value">{{ statistics.averageRating.toFixed(1) }}</div>
                <el-rate
                  v-model="statistics.averageRating"
                  disabled
                  show-score
                  text-color="#ff9900">
                </el-rate>
              </div>
              <div class="statistics-item">
                <div class="statistics-label">反馈数量</div>
                <div class="statistics-value">{{ statistics.totalFeedbacks }}</div>
              </div>
            </div>
            
            <div class="rating-distribution">
              <h4>评分分布</h4>
              <div v-for="i in 5" :key="i" class="rating-bar">
                <span class="rating-label">{{ i }}星</span>
                <el-progress 
                  :percentage="calculatePercentage(statistics.ratingCounts[i])" 
                  :color="getRatingColor(i)">
                  <span>{{ statistics.ratingCounts[i] || 0 }}人</span>
                </el-progress>
              </div>
            </div>
          </div>
          
          <el-empty v-else description="暂无反馈数据"></el-empty>
        </div>
      </el-tab-pane>
      
      <el-tab-pane label="反馈列表" name="list">
        <div v-loading="listLoading">
          <div v-if="feedbackList.length > 0">
            <el-timeline>
              <el-timeline-item
                v-for="item in feedbackList"
                :key="item.id"
                :timestamp="item.createTime | formatDateTime"
                placement="top">
                <el-card>
                  <div class="feedback-header">
                    <span class="feedback-user">{{ item.userName }}</span>
                    <el-rate v-model="item.rating" disabled></el-rate>
                  </div>
                  <div class="feedback-comment">{{ item.comment }}</div>
                </el-card>
              </el-timeline-item>
            </el-timeline>
            
            <!-- 分页 -->
            <div class="pagination-container">
              <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryParams.page"
                :page-sizes="[5, 10, 20]"
                :page-size="queryParams.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
              </el-pagination>
            </div>
          </div>
          
          <el-empty v-else description="暂无反馈数据"></el-empty>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { 
  getMeetingFeedbacks, 
  submitMeetingFeedback, 
  getMeetingFeedbackStatistics,
  hasSubmittedFeedback
} from '@/api/meeting/feedback'

export default {
  name: 'MeetingFeedback',
  props: {
    meetingId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      activeTab: 'submit',
      hasSubmitted: false,
      form: {
        rating: 5,
        comment: ''
      },
      rules: {
        rating: [
          { required: true, message: '请选择评分', trigger: 'change' }
        ],
        comment: [
          { required: true, message: '请输入评价内容', trigger: 'blur' }
        ]
      },
      statisticsLoading: false,
      statistics: {
        averageRating: 0,
        totalFeedbacks: 0,
        ratingCounts: {
          1: 0,
          2: 0,
          3: 0,
          4: 0,
          5: 0
        }
      },
      listLoading: false,
      feedbackList: [],
      total: 0,
      queryParams: {
        page: 1,
        size: 10
      }
    }
  },
  created() {
    this.checkSubmitted()
    this.getStatistics()
    this.getList()
  },
  methods: {
    checkSubmitted() {
      hasSubmittedFeedback(this.meetingId).then(response => {
        this.hasSubmitted = response.data
        if (this.hasSubmitted) {
          this.activeTab = 'statistics'
        }
      })
    },
    getStatistics() {
      this.statisticsLoading = true
      getMeetingFeedbackStatistics(this.meetingId).then(response => {
        this.statistics = response.data
        this.statisticsLoading = false
      })
    },
    getList() {
      this.listLoading = true
      getMeetingFeedbacks(
        this.meetingId, 
        this.queryParams.page, 
        this.queryParams.size
      ).then(response => {
        this.feedbackList = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const data = {
            meetingId: this.meetingId,
            rating: this.form.rating,
            comment: this.form.comment
          }
          
          submitMeetingFeedback(data).then(() => {
            this.$message.success('反馈提交成功')
            this.hasSubmitted = true
            this.activeTab = 'statistics'
            this.getStatistics()
            this.getList()
          })
        }
      })
    },
    calculatePercentage(count) {
      if (!count || this.statistics.totalFeedbacks === 0) {
        return 0
      }
      return Math.round((count / this.statistics.totalFeedbacks) * 100)
    },
    getRatingColor(rating) {
      const colors = {
        1: '#F56C6C',
        2: '#E6A23C',
        3: '#909399',
        4: '#67C23A',
        5: '#409EFF'
      }
      return colors[rating] || '#409EFF'
    },
    handleSizeChange(val) {
      this.queryParams.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.queryParams.page = val
      this.getList()
    }
  }
}
</script>

<style scoped>
.feedback-form {
  max-width: 600px;
  margin: 20px auto;
}
.statistics-container {
  padding: 20px;
}
.statistics-overview {
  display: flex;
  justify-content: space-around;
  margin-bottom: 30px;
}
.statistics-item {
  text-align: center;
}
.statistics-label {
  font-size: 14px;
  color: #606266;
  margin-bottom: 5px;
}
.statistics-value {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 5px;
}
.rating-distribution {
  max-width: 600px;
  margin: 0 auto;
}
.rating-bar {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
.rating-label {
  width: 50px;
  text-align: right;
  margin-right: 10px;
}
.feedback-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.feedback-user {
  font-weight: bold;
}
.feedback-comment {
  color: #606266;
  line-height: 1.6;
}
.pagination-container {
  margin-top: 20px;
  text-align: center;
}
</style> 